package com.xiaochong.tir.manager.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageInfo;
import com.xc.logclient.utils.LogTrace;
import com.xiaochong.tir.common.data.po.HbaseTableName;
import com.xiaochong.tir.common.exception.hbase.HbaseCloseExcetion;
import com.xiaochong.tir.common.exception.hbase.HbaseConnectionExcetion;
import com.xiaochong.tir.common.exception.hbase.HbaseGetTableExcetion;
import com.xiaochong.tir.manager.component.HbaseBaseQueryComponent;
import com.xiaochong.tir.manager.service.HbaseTableNameService;
import com.xiaochong.tir.manager.util.ManagerVersion;
import com.xiaochong.tir.manager.util.ReturnResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.*;

/**
 * Created by admin on 2018/5/12.
 */
@Api(value = "hbase数据查询")
@RestController
@RequestMapping(value = ManagerVersion.API_V1_PREFIX+"data/")
public class DictionaryController {

    @Autowired
    HbaseBaseQueryComponent hbQuery;
    @Autowired
    private HbaseTableNameService hbaseTableNameService;

    /**
     * 数据列表页面
     * @return
     */
    @GetMapping("dataPage")
    public ModelAndView dataPage(){
        return new ModelAndView("dataquery/dictionaryList");
    }

    /**
     * 数据列表
     * @param rowKey
     * @param tableName
     * @return
     */
    @ApiOperation(value = "数据列表",notes = "数据列表")
    @GetMapping("dataList")
    public Object getData(@RequestParam(value = "rowKey",required = false)String rowKey,
                          @RequestParam(value = "tableName",required = false)String tableName){
        Map<String,Object> map = new HashMap<String,Object>();
        try {
            if(StringUtils.isNotBlank(rowKey)&&StringUtils.isNotBlank(tableName)){
                map = hbQuery.selectData(tableName,rowKey);
            }
        } catch (HbaseConnectionExcetion hbaseConnectionExcetion) {
            hbaseConnectionExcetion.printStackTrace();
            LogTrace.error("数据查询",hbaseConnectionExcetion);
        } catch (HbaseGetTableExcetion hbaseGetTableExcetion) {
            hbaseGetTableExcetion.printStackTrace();
            LogTrace.error("数据查询",hbaseGetTableExcetion);
        } catch (IOException e) {
            e.printStackTrace();
            LogTrace.error("数据查询",e);
        } catch (HbaseCloseExcetion hbaseCloseExcetion) {
            hbaseCloseExcetion.printStackTrace();
            LogTrace.error("数据查询",hbaseCloseExcetion);
        }
        return new ReturnResult<Map<String,Object>>(0,"成功",map,0);
    }

    /**
     * 添加表名页面
     * @return
     */
    @GetMapping("addTable")
    public ModelAndView addTableName(){
        return new ModelAndView("dataquery/addDicPage");
    }

    /**
     * 查询下拉框列表
     * @return
     */
    @ApiOperation(value = "查询Hbase表名列表机提示",notes = "选择表名下拉框")
    @PostMapping("getTableNames")
    public Object getTableNames(){
        List<HbaseTableName> list = hbaseTableNameService.selectByEntityList(new HbaseTableName());
        for (int i = 0; i < list.size(); i++) {
            Map<String,String> map = new HashMap<>();
            if(StringUtils.isNotBlank(list.get(i).getRowkeyHint())){
                JSONArray array = JSONArray.parseArray(list.get(i).getRowkeyHint());
                for (int j = 0; j < array.size() ; j++) {
                    Map<String,String> json = JSONObject.fromObject(array.get(j));
                    List<String> vlist = new ArrayList<>(json.values());
                    map.put(vlist.get(1),vlist.get(0));
                }
            }
            list.get(i).setRowkeyHint(JSON.toJSONString(map));
        }
        return list;
    }

    /**
     * 添加表名
     * @param hbaseTableName
     * @return
     */
    @ApiOperation(value = "添加Hbase表名",notes = "添加Hbase表名")
    @PostMapping("addTableName")
    public Object addTable(HbaseTableName hbaseTableName){
        HbaseTableName tableName = hbaseTableNameService.selectByEntity(hbaseTableName);
        if(tableName != null){
            return new ModelAndView("dataquery/tnamelist");
        }else {
            hbaseTableNameService.insertSelective(hbaseTableName);
            return new ModelAndView("dataquery/tnamelist");
        }
    }

    /**
     * 列表页面
     * @return
     */
    @GetMapping("tablelist")
    public Object tabPage(){
        return new ModelAndView("dataquery/tnamelist");
    }

    /**
     * 列表
     * @param pageSize
     * @param pageIndex
     * @param hbaseTableName
     * @return
     */
    @ApiOperation(value = "查询Hbase表名列表",notes = "查询Hbase表名列表")
    @PostMapping("htablelist")
    public Object tabList(@RequestParam("limit") Integer pageSize,
                          @RequestParam("page")Integer pageIndex,
                          HbaseTableName hbaseTableName){
        PageInfo<HbaseTableName> info = hbaseTableNameService.selectByEntityLists(hbaseTableName,pageSize,pageIndex);
        ReturnResult<HbaseTableName> returnResult =
                new ReturnResult<HbaseTableName>(0,"成功",info.getList(),info.getTotal());
        return  returnResult;
    }

    /**
     * 编辑页面
     * @param id
     * @return
     */
    @GetMapping("editName")
    public Object updateTableName(@RequestParam("id") Integer id){
        HbaseTableName tableName = hbaseTableNameService.selectByPrimaryKey(id);
        ModelAndView mv = new ModelAndView();
        mv.addObject("tableName",tableName);
        mv.setViewName("dataquery/editDicPage");
        return mv;
    }

    /**
     * 编辑方法
     * @param hbaseTableName
     * @return
     */
    @ApiOperation(value = "编辑Hbase表名",notes = "编辑Hbase表名")
    @PostMapping("editTableName")
    public Object editTableName(HbaseTableName hbaseTableName){
        hbaseTableNameService.updateByPrimaryKey(hbaseTableName);
        return new ModelAndView("dataquery/tnamelist");
    }

    /**
     * 删除表名
     * @param id
     * @return
     */
    @ApiOperation(value = "删除Hbase表名",notes = "删除Hbase表名")
    @PostMapping("delTabName")
    public Object delTableName(@RequestParam("id") Integer id){
        hbaseTableNameService.deleteByPrimaryKey(id);
        return new ReturnResult<HbaseTableName>(0,"成功",0,0);
    }
}
